package org.rsbot.script.randoms;

import java.awt.Rectangle;
import java.util.logging.Logger;
import org.rsbot.gui.AccountManager;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Account;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Keyboard;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;

@ScriptManifest(authors={"Iscream", "Pervy Shuya"}, name="Login", version=1.6D)
public class LoginBot extends Random
{
  private static final int INTERFACE_MAIN = 905;
  private static final int INTERFACE_MAIN_CHILD = 59;
  private static final int INTERFACE_MAIN_CHILD_COMPONENT_ID = 4;
  private static final int INTERFACE_LOGIN_SCREEN = 596;
  private static final int INTERFACE_USERNAME = 65;
  private static final int INTERFACE_USERNAME_WINDOW = 37;
  private static final int INTERFACE_PASSWORD = 71;
  private static final int INTERFACE_PASSWORD_WINDOW = 39;
  private static final int INTERFACE_BUTTON_LOGIN = 42;
  private static final int INTERFACE_TEXT_RETURN = 11;
  private static final int INTERFACE_BUTTON_BACK = 55;
  private static final int INTERFACE_WELCOME_SCREEN = 906;
  private static final int INTERFACE_WELCOME_SCREEN_BUTTON_PLAY_1 = 160;
  private static final int INTERFACE_WELCOME_SCREEN_BUTTON_PLAY_2 = 171;
  private static final int INTERFACE_WELCOME_SCREEN_BUTTON_LOGOUT = 193;
  private static final int INTERFACE_WELCOME_SCREEN_TEXT_RETURN = 221;
  private static final int INTERFACE_WELCOME_SCREEN_BUTTON_BACK = 228;
  private static final int INTERFACE_WELCOME_SCREEN_HIGH_RISK_WORLD_TEXT = 86;
  private static final int INTERFACE_WELCOME_SCREEN_HIGH_RISK_WORLD_LOGIN_BUTTON = 93;
  private static final int INTERFACE_GRAPHICS_NOTICE = 976;
  private static final int INTERFACE_GRAPHICS_LEAVE_ALONE = 6;
  private static final int INDEX_LOGGED_OUT = 3;
  private static final int INDEX_LOBBY = 7;
  private int invalidCount;
  private int worldFullCount;

  public boolean activateCondition()
  {
    int i = this.game.getClientState();
    return ((i == 3) || (i == 7)) && (!switchingWorlds()) && (this.account.getName() != null);
  }

  public int loop()
  {
    String str1 = this.account.getName().toLowerCase().trim();
    String str2 = this.interfaces.get(596).getComponent(11).getText().toLowerCase();

    if (this.game.getClientState() != 3) {
      if (!this.game.isWelcomeScreen()) {
        sleep(random(1000, 2000));
      }
      if (this.game.getClientState() == 7) {
        RSInterface localRSInterface = this.interfaces.get(906);

        RSComponent localRSComponent1 = localRSInterface.getComponent(160);

        RSComponent localRSComponent2 = localRSInterface.getComponent(171);

        this.mouse.click(localRSComponent1.getAbsoluteX(), localRSComponent1.getAbsoluteY(), localRSComponent2.getAbsoluteX() + localRSComponent2.getWidth() - localRSComponent1.getAbsoluteX(), localRSComponent1.getHeight(), true);

        for (int k = 0; (k < 4) && (this.game.getClientState() == 6); k++) {
          sleep(500);
        }
        str2 = this.interfaces.get(906).getComponent(221).getText().toLowerCase();

        if ((str2.contains("total skill level of")) && (!this.account.isMember()))
        {
          log("Log back in when you total level of 1000+");
          this.interfaces.getComponent(906, 228).doClick();

          stopScript(false);
        } else if ((str2.contains("total skill level of")) && (this.account.isMember()))
        {
          log("Log back in when you total level of 1500+");
          this.interfaces.getComponent(906, 228).doClick();

          stopScript(false);
        }

        if (str2.contains("login limit exceeded")) {
          log("If login limit screws up & doesn't login, tell Pervy.");
          if (this.interfaces.getComponent(906, 228).doClick())
          {
            this.interfaces.getComponent(906, 193).doClick();
          }

        }

        if (str2.contains("your account has not logged out")) {
          this.log.warning("Make sure you're logged off or hasn't been h@x0r3d.");
          stopScript(false);
        }

        if (str2.contains("member")) {
          log("Unable to login to a members world. Stopping script.");
          RSComponent localRSComponent3 = this.interfaces.get(906).getComponent(228);

          RSComponent localRSComponent4 = this.interfaces.get(906).getComponent(231);

          this.mouse.click(localRSComponent3.getAbsoluteX(), localRSComponent3.getAbsoluteY(), localRSComponent4.getAbsoluteX() + localRSComponent4.getWidth() - localRSComponent3.getAbsoluteX(), localRSComponent3.getHeight(), true);

          this.interfaces.get(906).getComponent(203).doClick();

          stopScript(false);
        }

        if (this.interfaces.get(906).getComponent(86).getText().toLowerCase().trim().contains("high-risk wilderness world"))
        {
          this.interfaces.get(906).getComponent(93).doClick();
        }

      }

      return -1;
    }
    if (!this.game.isLoggedIn()) {
      if (str2.contains("no reply from login server")) {
        if (this.invalidCount > 10) {
          this.log.warning("Unable to login after 10 attempts. Stopping script.");
          this.log.severe("It seems the login server is down.");
          stopScript(false);
        }
        this.interfaces.get(596).getComponent(55).doClick();

        this.invalidCount += 1;
        return random(500, 2000);
      }
      if (str2.contains("update")) {
        log("Runescape has been updated, please reload RSBot.");
        stopScript(false);
      }
      if (str2.contains("disable")) {
        this.log.severe("Your account is banned/disabled.");
        stopScript(false);
      }
      if (str2.contains("your account has not logged out")) {
        this.log.warning("Make sure you're logged off or hasn't been h@x0r3d.");
        stopScript(false);
      }
      if (str2.contains("incorrect")) {
        this.log.warning("Failed to login five times in a row. Stopping script.");
        stopScript(false);
      }
      if (str2.contains("invalid")) {
        if (this.invalidCount > 6) {
          this.log.warning("Unable to login after 6 attempts. Stopping script.");
          log("Please verify that your RSBot account profile is correct.");
          stopScript(false);
        }
        this.interfaces.get(596).getComponent(55).doClick();

        this.invalidCount += 1;
        return random(500, 2000);
      }
      if (str2.contains("error connecting")) {
        this.interfaces.get(596).getComponent(55).doClick();

        stopScript(false);
        return random(500, 2000);
      }
      if (str2.contains("full")) {
        if (this.worldFullCount > 30) {
          log("World Is Full. Waiting for 15 seconds.");
          sleep(random(10000, 15000));
          this.worldFullCount = 0;
        }
        sleep(random(1000, 1200));
        this.worldFullCount += 1;
      }
      if (str2.contains("world")) {
        return random(1500, 2000);
      }
      if (str2.contains("performing login")) {
        return random(1500, 2000);
      }
    }
    if (this.game.getClientState() == 3) {
      if (this.interfaces.getComponent(976, 6).isValid())
      {
        this.interfaces.getComponent(976, 6).doClick();

        return random(500, 600);
      }
      if (!atLoginScreen()) {
        this.interfaces.getComponent(905, 59).getComponent(4).doAction("");

        return random(500, 600);
      }
      if ((isUsernameFilled()) && (isPasswordFilled())) {
        if (random(0, 2) == 0)
          this.keyboard.pressKey('\n');
        else {
          this.interfaces.get(596).getComponent(42).doClick();
        }
        return random(500, 600);
      }
      int i;
      int j;
      if (!isUsernameFilled()) {
        atLoginInterface(this.interfaces.get(596).getComponent(37));

        sleep(random(500, 700));
        i = this.interfaces.get(596).getComponent(65).getText().length() + random(3, 5);

        for (j = 0; j <= i + random(1, 5); j++) {
          this.keyboard.sendText("\b", false);
          if (random(0, 2) == 1) {
            sleep(random(25, 100));
          }
        }
        this.keyboard.sendText(str1, false);
        return random(500, 600);
      }
      if ((isUsernameFilled()) && (!isPasswordFilled())) {
        atLoginInterface(this.interfaces.get(596).getComponent(39));

        sleep(random(500, 700));
        i = this.interfaces.get(596).getComponent(71).getText().length() + random(3, 5);

        for (j = 0; j <= i + random(1, 5); j++) {
          this.keyboard.sendText("\b", false);
          if (random(0, 2) == 1) {
            sleep(random(25, 100));
          }
        }
        this.keyboard.sendText(AccountManager.getPassword(this.account.getName(), getClass()), false);
      }
    }

    return random(500, 2000);
  }

  private boolean switchingWorlds() {
    return (this.interfaces.get(906).getComponent(221).isValid()) && (this.interfaces.get(906).getComponent(221).containsText("just left another world"));
  }

  private boolean atLoginInterface(RSComponent paramRSComponent)
  {
    if (!paramRSComponent.isValid())
      return false;
    Rectangle localRectangle = paramRSComponent.getArea();
    if ((localRectangle.x == -1) || (localRectangle.y == -1) || (localRectangle.width == -1) || (localRectangle.height == -1))
      return false;
    int i = (int)(localRectangle.getHeight() - 4.0D) / 2;
    int j = (int)(localRectangle.getMaxX() - localRectangle.getCenterX());
    int k = (int)localRectangle.getCenterX();
    int m = (int)(localRectangle.getMinY() + localRectangle.getHeight() / 2.0D);
    if (paramRSComponent.getIndex() == 39)
      this.mouse.click(minX(paramRSComponent), m + random(-i, i), true);
    else {
      this.mouse.click(k + random(1, j), m + random(-i, i), true);
    }

    return true;
  }

  private int minX(RSComponent paramRSComponent)
  {
    int i = 0;
    Rectangle localRectangle = paramRSComponent.getArea();
    int j = (int)(localRectangle.getWidth() - 4.0D) / 2;
    int k = (int)(localRectangle.getMinX() + localRectangle.getWidth() / 2.0D);
    if ((localRectangle.x == -1) || (localRectangle.y == -1) || (localRectangle.width == -1) || (localRectangle.height == -1))
      return 0;
    for (int m = 0; m < this.interfaces.get(596).getComponent(71).getText().length(); )
    {
      i += 11;

      m++;
    }

    if (i > 44) {
      return (int)(localRectangle.getMinX() + i + 15.0D);
    }
    return k + random(-j, j);
  }

  private boolean atLoginScreen()
  {
    return this.interfaces.get(596).isValid();
  }

  private boolean isUsernameFilled() {
    String str = this.account.getName().toLowerCase().trim();
    return this.interfaces.get(596).getComponent(65).getText().toLowerCase().equalsIgnoreCase(str);
  }

  private boolean isPasswordFilled()
  {
    String str = AccountManager.getPassword(this.account.getName(), getClass());

    return this.interfaces.get(596).getComponent(71).getText().toLowerCase().length() == (str == null ? 0 : str.length());
  }
}